﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Tebbee.Business.Server
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MediaTypeSer" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MediaTypeSer.svc or MediaTypeSer.svc.cs at the Solution Explorer and start debugging.
    public class MediaTypeSer : IMediaTypeSer
    {
        private readonly TebbeeContext context;

        public MediaTypeSer()
        {
            context = new TebbeeContext();
        }
        public int AddNewMediaType(MediaType mt)
        {
            ObjectParameter ReturnId = new ObjectParameter("ReturnId", typeof(Int32));
            context.MediaType_Add(mt.Name, mt.Description, mt.IconPath, mt.Mime, ReturnId);
            return (Int32)ReturnId.Value;
        }

        public ICollection<MediaType> ListMediaTypes(int startRow, string searchString)
        {
            var returnCat = new List<MediaType>();
            context.MediaType_List(startRow, searchString).ToList().ForEach(e => returnCat.Add(new MediaType()
            {
                MediaTypeId = e.MediaTypeId,
                Description = e.Description,
                IconPath = e.IconPath,
                Name = e.Name,
                //Mime = e.Mime
            }));

            return returnCat;
        }

        public int EditMediaType(MediaType mt)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.MediaType_Edit(mt.MediaTypeId, mt.Name, mt.Description, mt.IconPath, mt.Mime, rowEffect);
            return (Int32)rowEffect.Value;
        }

        public int DeleteMediaType(int id)
        {
            ObjectParameter rowEffect = new ObjectParameter("RowEffected", typeof(Int32));
            context.MediaType_Delete(id, rowEffect);
            return (Int32)rowEffect.Value;
        }


        public ICollection<MediaType> AllMediaTypes()
        {
            var returnCat = new List<MediaType>();
            context.MediaType_All().ToList().ForEach(e => returnCat.Add(new MediaType()
            {
                MediaTypeId = e.MediaTypeId,
                Description = e.Description,
                IconPath = e.IconPath,
                Name = e.Name,
                Mime = e.Mime
            }));

            return returnCat;
        }
    }
}
